package pl.edu.agh.hollonCommision.graph;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.IFigure;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.zest.core.viewers.IFigureProvider;
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;


public class ZestGraph extends LabelProvider implements IGraphEntityContentProvider, IFigureProvider{

	private GraphModel model;
	public ZestGraph(GraphModel model) {
		super();
		this.model = model;
	}

	@Override
	public Object[] getConnectedTo(Object entity) {
		if(entity instanceof Vertex){
			Vertex v= (Vertex)entity;
			List<Edge> edges = model.getEdges().get(v.getID());
			if(null == edges){
				return null;
			}
			Vertex[] vs = new Vertex[edges.size()];
			for(int i = 0; i < edges.size(); i++){
				vs[i] = edges.get(i).getV2();
			}
			return vs;
		}
		return null;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		Map<Integer, Vertex> vertices = model.getVertices();
		Vertex[] vs = new Vertex[vertices.size()];
		vertices.values().toArray(vs);
		return vs;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub

	}
	
	public String getText(Object entity){
		if(entity instanceof Vertex){
			Vertex v = (Vertex)entity;
			return new String(v.getID() + "( "+v.getCommision().getDeliveryX() +", " + v.getCommision().getDeliveryY()+" )");
		}else{
			//System.out.println(entity.getClass());
		}
		return null;
	}

	@Override
	public IFigure getFigure(Object element) {
		IFigure fig = new Ellipse();
		fig.setForegroundColor(new Color(Display.getCurrent(), 255, 0, 0));
		fig.setBackgroundColor(new Color(Display.getCurrent(), 200, 0, 0));
		int size = (int) Math.ceil(20.0 * ((Vertex) element).getCurrentMetricNormalizedValue()) + 3;
		fig.setSize(size, size);
		return fig;
	}
	

}
